/**
 * Source file: Application For Funeral Claim Form_AutoLookups.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id$
 * Date: $Date$
 */
 
//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in ConnectWebService (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

$(document).ready(function() {

/*
Application For Funeral Claim Form Field List for AutoComplete

f190 Occupation of the Deceased

f98 Mainmember Suburb
f104 Mainmember Ciy
f112 Minmember PostalCode 

f167 Deceased Suburb
f176 Deceased City
f187 Deceased PostalCode

f247 Hospital Suburb
f249 Hospital City
f251 Hospital Postal Code

f43 Tribal Chief Suburb
f48 Tribal Chief Town
f56 Tribal Chief PostalCode

f87 Funeral Parlour Suburb
f95 Funeral Parlour Town
f101 Funeral Parlour PostalCode

f123 Physical Address Suburb
f129 Physical Address Town
f137 Physical Address PostalCode

f206 School Suburb
f210 School Town
f216 School PostalCode

f47 Claimant Suburb
f52 Claimant Town
f60 Claimant PostalCode

f142 Payment Details BankCode - Hidden Field
f132 Payment Details Bank
f133 Payment Details BranchName
f134 Payment Details BranchCode

f151 Usual family Doctor Suburb 
f157 Usual Family Doctor Town
f164 Usual Family Doctor PostalCode
*/
	// the field id is generated by Electric paper and should be changed to the apporiate value 
	// if the form is changed (fields added or deleted) this id could also change
	// the best way to locate the id is to right click on the field in firefox and select "Inspect Element"

//Main Member Address
$("#f104").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f98").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f104').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f112').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f112").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f104').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f98').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Deceased Address Details
$("#f176").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f167").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f176').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f187').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f187").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f176').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f167').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Deceased Occupation
$("#f190").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});
		
//Hospital Address Details
$("#f249").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f247").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f249').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f251').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f251").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f249').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f247').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Tribal Chief Address Details
$("#f48").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f43").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f48').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f56').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f56").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f48').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f43').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Funeral Parlour Address Details
$("#f95").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f87").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f95').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f101').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f101").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f95').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f87').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Usual Doctor Address Details
$("#f157").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f151").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f157').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f164').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f164").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f157').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f151').value = $.trim(pieces[pieces.length-1]); }		
		});		

//School/Employer Address Details
$("#f210").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f206").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f210').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f216').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f216").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f210').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f206').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Claimant Address Details
$("#f52").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f47").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f52').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f60').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f60").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f52').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f47').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Payment Bank and Branch Details
//Payment Bank and Branch Details

$("#f142").autocomplete({ //Display BankCode in a Different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BankId"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f132').value = $.trim(pieces[pieces.length-1]); }
		});
		


$("#f132").autocomplete({ //Display BankCode in a Different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BankName"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f142').value = $.trim(pieces[pieces.length-1]); }
		});


$("#f133").autocomplete({ // display BranchCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BranchName","parent":"' + document.getElementById('f132').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f134').value = $.trim(pieces[pieces.length-1]); }		
		});

$("#f134").autocomplete({ // display BrancName in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BranchCode","parent":"' + document.getElementById('f132').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f133').value = $.trim(pieces[pieces.length-1]); }				
		});	
});

